package com.zy.config;

import com.zy.enumcfg.ResponseStatusEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.zy")).paths(PathSelectors.any())
                .build()
                .globalResponseMessage(RequestMethod.GET, getMsg())
                .globalResponseMessage(RequestMethod.POST, getMsg())
                .globalResponseMessage(RequestMethod.PUT, getMsg())
                .globalResponseMessage(RequestMethod.DELETE, getMsg())
                .globalResponseMessage(RequestMethod.HEAD, getMsg())
                .globalResponseMessage(RequestMethod.PATCH, getMsg())
                .globalResponseMessage(RequestMethod.OPTIONS, getMsg());
    }





    // 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        List<VendorExtension> vendorExtensions = new ArrayList<>();
        vendorExtensions.add(new VendorExtension() {
            @Override
            public String getName() {
                return "vendorExtensions Name";
            }

            @Override
            public Object getValue() {
                return null;
            }
        });
        return new ApiInfoBuilder()
                // 页面标题
                .title("Spring Boot 使用 Swagger2 构建RESTful API")
                // 创建人信息
                .contact(new Contact("MrZZ",  "https://www.cnblogs.com/www.html",  "87878777@666.com"))
                // 版本号
                .version("1.0")
                // 描述
                .description("API description222333")
                .license("【license】")
                .licenseUrl("【licenseUrl】")
                .termsOfServiceUrl("【termsOfServiceUrl】")
                .extensions(vendorExtensions)
                .build();
    }



    private List<ResponseMessage> getMsg(){
//        responseMessageList.add(new ResponseMessageBuilder().code(404).message("找不到资源").responseModel(new ModelRef("ApiError")).build());

        List<ResponseMessage> responseMessageList = new ArrayList<>();

        for (ResponseStatusEnum value : ResponseStatusEnum.values()) {

            responseMessageList.add(new ResponseMessageBuilder().code(value.getCode()).message(value.getMsg()).build());

        }

        return responseMessageList;

    }


}
